import request from '@u/request'

export function queryTotal(data, special) {
  let url = `/data/total/query`
  if (special) {
    url = `/data/total/satisfaction/query`
  }
  return request(url, {
    method: 'GET',
    params: data,
  })
}

export function queryArea(data) {
  const { level, id, districtFlag,currentId } = data
  const params = new URLSearchParams()
  params.append('level', level || '')
  params.append('id', id || '')
  params.append('districtFlag', districtFlag || '')
  params.append('currentId', currentId || '')
  return request(`/resource/area/query?${params.toString()}`, {
    method: 'GET',
  })
}

export function querySatisfactionArea(data) {
  const { level, id } = data
  const params = new URLSearchParams()
  params.append('level', level || '')
  params.append('id', id || '')
  return request(`/resource/area/satisfaction/region?${params.toString()}`, {
    method: 'GET',
  })
}

// 查询主城九区的id
export function querySpecialIds() {
  return request(`/topic/total/major/id`, {
    method: 'GET',
  })
}

export function queryTaskArea(baseId) {
  return request(`/index/task/queryRegion?baseId=${baseId}`, {
    method: 'GET',
  })
}

/**
 * 查询问题
 * @param {Object} data - 查询参数
 * @param {string} data.templateType - 模板类型
 * @param {string} data.businessId - 关联业务模块id
 * @param {string} data.communityId - 社区id
 * @param {string} data.streetId - 街道id
 * @returns {Promise} 返回一个Promise对象，其解析值为查询结果
 */
export function queryQuestion(data) {
  const {
    templateType, // 模板类型
    businessId, // 关联业务模块id
    communityId, //社区id
    streetId, // 街道id
    phone,
  } = data
  // 根据参数组装url
  return request(
    `/topic/template/query/entirety?templateType=${templateType}&businessId=${
      businessId ?? ''
    }&communityId=${communityId ?? ''}&streetId=${streetId ?? ''}&phone=${
      phone ?? ''
    }`,
    {
      method: 'GET',
    }
  )
}

export function getRegionDetail(params) {
  return request('/topic/region/business/get', {
    method: 'GET',
    params,
  })
}

export function getResidenceTree(params) {
  return request('/resource/area/queryResidenceTree', {
    method: 'GET',
    params,
  })
}

/**
 * 查询社区/街道/区所属小区列表
 * @param {Object} data - 查询参数
 * @param {string} data.regionId - 区域ID
 * @param {string} data.streetId - 街道ID
 * @param {string} data.communityId - 社区ID
 * @param {string} data.type - 模板类型
 * @param {string} data.status - 状态
 * @param {number} data.pageSize - 每页数量
 * @param {number} data.pageIndex - 页码
 * @returns {Promise} 返回一个Promise对象，其解析值为查询结果
 */
export function queryList(data, _type = 'residence') {
  // console.log(data)
  const {
    regionId,
    streetId,
    communityId,
    type, // (模板类型)
    status, //（状态）
    pageSize,
    pageIndex,
  } = data

  const params = new URLSearchParams()
  params.append('regionId', regionId || '')
  params.append('streetId', streetId || '')
  params.append('communityId', communityId || '')
  params.append('type', _type || '')
  params.append('status', status ?? '')
  params.append('pageSize', pageSize || 999)
  params.append('pageIndex', pageIndex || 1)

  let requestUrl = `/topic/business/query/residence?${params.toString()}`

  if (_type !== 'residence') {
    requestUrl = `/topic/business/query/community?${params.toString()}`
  }

  return request(requestUrl, {
    method: 'GET',
  })
}

export function queryRegionList(params) {
  let url = '/topic/region/business/query'
  if (params.type === 'region-community') {
    url = '/topic/region/business/community/query'
  }
  return request(url, {
    method: 'GET',
    params: params,
  })
}

export function importRegionBusiness(data) {
  return request('/topic/region/business/import', {
    method: 'POST',
    // formData格式
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data,
  })
}

/**
 * 查询一张图
 * @returns {Promise<{
 *     success: boolean,
 *     message: string,
 *     data: Object | null | Array
 * }>} 返回一个Promise对象，其解析值为查询结果
 * @param type string 查询类型
 * @param params object 查询参数
 */
export function getLayerSourceByType(type, params) {
  return request(`/topic/map/query/${type}`, {
    method: 'GET',
    params,
  })
}

/**
 * 查询区县特殊一张图
 * @returns {Promise<{
 *     success: boolean,
 *     message: string,
 *     data: Object | null | Array
 * }>} 返回一个Promise对象，其解析值为查询结果
 * @param type string 查询类型
 * @param params object 查询参数
 */
export function getSpecialRegionArea(type, params) {
  return request(`/topic/map/query/region/special`, {
    method: 'GET',
    params,
  })
}

export function getLayerSourceCommunityLiveRange(key, params) {
  return request(`/topic/map/query/community/live/range`, {
    method: 'GET',
    params,
  })
}

/**
 * 查询一张图
 * @returns {Promise<{
 *     success: boolean,
 *     message: string,
 *     data: Object | null | Array
 * }>} 返回一个Promise对象，其解析值为查询结果
 * @param params object 查询参数
 */
export function getLayerSourceCommunityRegion(key, params) {
  return request(`/topic/map/query/community/area`, {
    method: 'GET',
    params,
  })
}

// const baseFileUrl =
//   process.env.NODE_ENV === 'development'
//     ? 'http://192.168.3.37:3112'
//     : window.BASE_FILE_URL

export function uploadFile(data) {
  return request('/resource/file/upload/file', {
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data,
  })
}

export function uploadFiles(data) {
  return request('/resource/file/upload/files', {
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data,
  })
}
// 删除上传文件
export function deleteFile(data) {
  return request(`/resource/file/delete?resourceId=${data.resourceId}`, {
    method: 'DELETE',
  })
}

export function downloadFile(id) {
  return request('/resource/file/download?pkid=' + id, {
    method: 'GET',
    responseType: 'blob',
  })
}

// 查询业务模块状态
export function queryStatus(data) {
  const { relationId, type, pkid } = data
  const params = new URLSearchParams()
  params.append('relationId', relationId || '')
  params.append('type', type || '')
  params.append('pkid', pkid || '')
  return request(`/topic/business/query/status?${params.toString()}`, {
    method: 'GET',
  })
}

export function queryChoiceBuild(params) {
  return request(`/topic/map/query/build`, {
    method: 'GET',
    params,
  })
}

export function addApplyRecord(data) {
  const requestUrl = `/topic/core/apply/add`
  return request(requestUrl, {
    method: 'POST',
    data,
  })
}

export function backBatch(data) {
  const requestUrl = `/topic/core/apply/backBatch`
  return request(requestUrl, {
    method: 'POST',
    data,
  })
}
// 查询右侧目录树
export function queryRightTree(data) {
  return request('/data/template/query/template', {
    method: 'GET',
    params: data,
  })
}
// 左侧列表
export function queryLeftList(data) {
  return request('/data/map/query', {
    method: 'GET',
    params: data,
  })
}
// 通过问卷id查询问题
export function queryQuestionById(data) {
  return request('/data/problem/query', {
    method: 'GET',
    params: data,
  })
}
// 答案新增更新
export function addOrUpdateAnswer(data) {
  return request('/data/map/answer/addOrUpdate', {
    method: 'POST',
    data,
  })
}
// 答案删除
export function deleteAnswer(data) {
  return request('/data/template/result/delete', {
    method: 'POST',
    data,
  })
}
// 一张图上传获取gdb属性
export function getGdbProperty(data) {
  return request('/data/map/gdb/property/query', {
    method: 'GET',
    params: data,
  })
}
// 一张图上传gdb
export function uploadGdb(data) {
  return request('/data/map/gdb/property/import', {
    method: 'POST',
    data,
  })
}

// 一张图上传gdb
export function uploadGdbSq(data) {
  return request('/data/map/gdb/left/property/import', {
    method: 'POST',
    data,
  })
}

// 根据右侧目录树查询地图
export function getLayerSourceByCatalogId(data) {
  return request('/data/map/map/query', {
    method: 'GET',
    params: data,
  })
}

// 下载
export function downloadFileById(data) {
  return request('/data/map/gdb/download', {
    method: 'GET',
    params: data,
    responseType: 'blob'
  })
}

export function getLayerLocateByBuildingId(data) {
  return request('/data/share/building/location', {
    method: 'POST',
    data,
  })
}

// 下载满意度调查问卷
export function downloadSatisfaction(data) {
  return request('/data/export/excel/download', {
    method: 'GET',
    params: data,
    responseType: 'blob'
  })
}